import { defineEventHandler, getCookie, readBody, createError } from 'h3';
import { CloudBackupManager } from '../../utils/cloudBackup.js';

export default defineEventHandler(async (event) => {
  try {
    const userId = getCookie(event, 'userId');
    if (!userId) {
      throw createError({ statusCode: 401, statusMessage: 'Unauthorized' });
    }

    const body = await readBody(event);
    const { cloudConfig } = body;
    
    if (!cloudConfig) {
      throw createError({ statusCode: 400, statusMessage: 'Cloud configuration is required' });
    }

    // 创建云备份管理器
    const cloudManager = new CloudBackupManager(cloudConfig);
    
    // 列出云端备份
    const backups = await cloudManager.listCloudBackups();

    return {
      success: true,
      backups: backups
    };
  } catch (error) {
    console.error('列出云端备份失败:', error);
    throw createError({ statusCode: 500, statusMessage: 'Failed to list cloud backups' });
  }
});
